#include <Windows.h>
#include <string>
#include <sstream>
#include <windows.h>
#include <winuser.h>
#include <winresrc.h>
using namespace std;
const int TIMER_ID = 1;
const int WINDOW_WIDTH = 200;
const int WINDOW_HEIGHT = 100;
const int COUNTDOWN_INTERVAL = 1000 * 60 * 10; // 10分钟倒计时，单位为毫秒

HWND hwnd, hDragControl;
int countdown = 0;
bool isCountingDown = false;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
        case WM_CREATE:
            SetTimer(hwnd, TIMER_ID, 1000, NULL); // 每隔1秒触发计时器

            // 创建一个用于拖动窗口的控件
            hDragControl = CreateWindow("STATIC", NULL, WS_CHILD | WS_VISIBLE | SS_BLACKRECT,
                                        0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, hwnd, NULL, NULL, NULL);
            break;
        case WM_TIMER:
            if (wParam == TIMER_ID) {
                if (isCountingDown) {
                    countdown -= 1000;
                    if (countdown <= 0) {
                        countdown = COUNTDOWN_INTERVAL;
                    }
                    ostringstream oss;
                    oss << "time: " << countdown / 1000 << " s";
//                    std::string text = "倒计时: " + std::to_string(countdown / 1000) + " 秒";
                    SetWindowTextA(hwnd, oss.str().c_str());
                }
            }
            break;
        case WM_PAINT: {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);
            RECT clientRect;
            GetClientRect(hwnd, &clientRect);
            DrawTextA(hdc, "倒计时器", -1, &clientRect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            EndPaint(hwnd, &ps);
            break;
        }
        case WM_CLOSE:
            DestroyWindow(hwnd);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        case WM_LBUTTONDOWN:
            SendMessage(hwnd, WM_SYSCOMMAND, SC_MOVE | HTCAPTION, 0);
            break;
        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    const char *className = "CountdownTimer";
    WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_HREDRAW | CS_VREDRAW, WindowProc,
                     0, 0, hInstance, NULL, LoadCursor(NULL, IDC_ARROW),
                     NULL, NULL, className, NULL};
    RegisterClassEx(&wc);
    hwnd = CreateWindowEx(
            WS_EX_LAYERED | WS_EX_TOPMOST | WS_EX_TRANSPARENT, // 创建一个透明、置顶、无边框的窗口，且鼠标穿透
            className, "倒计时器", WS_POPUPWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, hInstance, NULL);
    if (hwnd == NULL) {
        return 0;
    }
    SetLayeredWindowAttributes(hwnd, RGB(0, 0, 0), 100, LWA_COLORKEY | LWA_ALPHA); // 设置为透明窗口

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
    countdown = COUNTDOWN_INTERVAL;
    isCountingDown = true;
    ostringstream oss;
    oss << "time: " << countdown / 1000 << " s";
//    std::string text = "倒计时: " + std::to_string(countdown / 1000) + " 秒";
    SetWindowTextA(hwnd, oss.str().c_str());
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        if (!IsDialogMessage(hwnd, &msg)) // 让消息能够在拖动控件上正常处理
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return msg.wParam;
}